package com.lanchetech.admin.service.impl;


import com.alibaba.excel.util.CollectionUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanchetech.admin.service.DiseaseService;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.util.PageHelpUtil;
import com.lanchetech.bean.vo.DiseaseReportVO;
import com.lanchetech.common.enums.DeletedEnum;
import com.lanchetech.dao.DiseaseMapper;
import com.lanchetech.dao.DiseaseReportHistoryMapper;
import com.lanchetech.dao.DiseaseReportMapper;
import com.lanchetech.entity.Disease;
import com.lanchetech.entity.DiseaseReport;
import com.lanchetech.entity.DiseaseReportHistory;
import com.lanchetech.entity.User;
import com.lanchetech.service.DaoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class DiseaseServiceImpl implements DiseaseService {

    @Autowired
    DiseaseMapper diseaseMapper;

    @Autowired
    DiseaseReportMapper diseaseReportMapper;

    @Autowired
    DaoService daoService;

    @Autowired
    DiseaseReportHistoryMapper diseaseReportHistoryMapper;

    @Override
    public BaseResp editDisease(Disease req) {
        if(req.getId() == null){
            req.setDeleted(DeletedEnum.DEFAULT.getStatus());
            req.setCreatedAt(new Date());
            req.setUpdatedAt(new Date());
            diseaseMapper.insert(req);
        }else{
            diseaseMapper.updateByPrimaryKeySelective(req);
        }
        return new BaseResp();
    }

    @Override
    public ResultData<BasePageResp<Disease>> getDiseasePage(PageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<Disease> list = diseaseMapper.findAllPage(req);
        return PageHelpUtil.buildPage(list, page);
    }

    @Override
    public ResultData<Disease> getDiseaseDetail(Long id) {
        return new ResultData<>(diseaseMapper.selectByPrimaryKey(id));
    }

    @Override
    public ResultData<BasePageResp<DiseaseReportVO>> getDiseaseReportPage(PageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<DiseaseReport> list = diseaseReportMapper.findAllPage(req);
        List<DiseaseReportVO> result = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            Map<Long, User> userMap = daoService.getUserMap(list.stream().map(DiseaseReport::getUserId).collect(Collectors.toList()));
            list.stream().forEach(item->{
                DiseaseReportVO vo = new DiseaseReportVO();
                BeanUtils.copyProperties(item, vo);
                vo.setAvatar(userMap.get(item.getUserId()).getAvatar());
                vo.setPhone(userMap.get(item.getUserId()).getUsername());
                result.add(vo);
            });
        }
        return PageHelpUtil.buildPage(result, page);
    }

    @Override
    public ResultData<BasePageResp<DiseaseReportHistory>> getDiseaseReportHistory(PageReq req) {
        Page page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<DiseaseReportHistory> list = diseaseReportHistoryMapper.findAllPage(req);
        return PageHelpUtil.buildPage(list, page);
    }

    @Override
    public BaseResp deleteDisease(Long id) {
        diseaseMapper.updateDeletedById(DeletedEnum.DELETED.getStatus(), id);
        return new BaseResp();
    }
}